<template>
	<view class="container">
		<view class="">
			<u-subsection fontSize="14px" :list="list" :current="curNow" @change="sectionChange" bgColor="#3c9cff">
			</u-subsection>
		</view>

		<view>
			<u--form labelPosition="top" :model="userInfo" :rules="rules" ref="uForm">
				<u-form-item required label="伙伴姓名" prop="nickName" borderBottom  labelWidth="200px">
					<u--input v-model="userInfo.nickName" border="none"></u--input>
				</u-form-item>

				<u-form-item required label="登录手机号" prop="userName" borderBottom  labelWidth="200px">
					<u--input v-model="userInfo.userName" border="none"></u--input>
				</u-form-item>
				
				<u-form-item required label="代理" prop="agent" borderBottom labelWidth="200px">
					<u-radio-group v-model="inintAgent" placement="row">
						<u-radio :customStyle="{margin: '8px'}" v-for="(item, index) in agentValue"
							:key="index" :label="item.name" :name="item.name" @change="agentValueChange(index)">
						</u-radio>
					</u-radio-group>
				</u-form-item>

				<u-button type="primary" @click="submit">提交</u-button>
			</u--form>
		</view>
	</view>


</template>

<script>
	import {addPartner} from '@/api/system/user.js'
	export default {

 // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},

		data() {
			return {
				list: ['添加伙伴', '查找伙伴'],
				curNow: 0,

				userInfo: {
					nickName: '',
					userName: '',
					agent:0,
				},

				agentValue: [{
					name: '省',
					disabled: true
				}, {
					name: '市',
					disabled: false
				}, {
					name: '县',
					disabled: false
				}],
				inintAgent: '县',
				rules: {
					'nickName': [{
						type: 'string',
						required: true,
						message: '身份证姓名',
						max: 8,
						trigger: ['blur', 'change']
					}],
					'userName': [{
						type: 'number',
						required: true,
						// min: 11,
						// max: 11,
						message: '请输入11位手机号',
						trigger: ['blur', 'change']
					}],
				},
			}
		},
		methods: {
			agentValueChange(n){
				console.log('agentValueChange', n);
				this.userInfo.agent = n;
			}
			,
			sectionChange(index) {
				let oldCurNow = this.curNow;
				this.curNow = index;
				if (index === 1 && oldCurNow === 0) {
					uni.redirectTo({
						url: '/pages/partner/search'
					})
				}
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					//console.log(JSON.stringify(this.userInfo))
					addPartner(this.userInfo).then(res =>{
						console.log(JSON.stringify(res))
						this.$modal.alert(res.msg)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败' + JSON.stringify(errors))
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		width: 92%;
		display: flex;
		flex-direction: column;
		margin: auto;
	}
</style>